#pragma once
#include<initializer_list>
#include<algorithm>

using namespace std;

namespace wx
{
	template<class T>
	struct List_node
	{
		T _data;
		List_node<T>* _prev;
		List_node<T>* _next;

		List_node(const T& data = T())
			:_data(data), _prev(nullptr), _next(nullptr) {
		}

	};

	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef List_node<T> Node;
		typedef __list_iterator<T, Ref, Ptr> self;
		Node* _node;

		__list_iterator(Node* node)
			:_node(node) {
		}

		self& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		self operator++(int)
		{
			self tem(*this);
			_node = _node->_next;
			return tem;
		}

		self& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		self operator--(int)
		{
			self tem(*this);
			_node = _node->_prev;
			return tem;
		}

		Ptr operator->()
		{
			return &(_node->_data);
		}

		Ref operator*()
		{
			return _node->_data;
		}

		bool operator!=(const self& it) const
		{
			return (_node != it._node);
		}

		bool operator==(const self& it) const
		{
			return (_node == it._node);
		}
	};

	template<class Iterator, class Ref, class Ptr>
	struct Reverse_iterator
	{
		typedef Reverse_iterator<Iterator, Ref, Ptr> self;

		Reverse_iterator(Iterator it)
			:_it(it){ }

		self& operator++()
		{
			--_it;
			return *this;
		}

		self operator++(int)
		{
			self tem(*this);
			--_it;
			return tem;
		}

		self& operator--()
		{
			++_it;
			return *this;
		}

		self operator--(int)
		{
			self tem(*this);
			++_it;
			return tem;
		}

		bool operator==(const self& it) const
		{
			return _it == it._it;
		}

		bool operator!=(const self& it) const
		{
			return _it != it._it;
		}

		Ref operator*()
		{
			Iterator temp = _it;
			return *--temp;
		}

		Ptr operator->()
		{
			return &(operator*());
		}

		Iterator _it;
	};

	template<class T>
	class list
	{
		typedef List_node<T> Node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;
		template<class Iterator, class Ref, class Ptr>
		struct Reverse_iterator;
		typedef Reverse_iterator<iterator, T&, T*> Reverse_iterator;
		typedef Reverse_iterator<const_iterator, const T&, const T*> const_Reverse_iterator;

		list()
		{
			empty();
		}

		Reverse_iterator rbegin()
		{
			return Reverse_iterator(end());
		}

		const_Reverse_iterator rbegin() const
		{
			return const_Reverse_iterator(end());
		}

		Reverse_iterator rend()
		{
			return Reverse_iterator(begin());
		}

		const_Reverse_iterator rend() const
		{
			return const_Reverse_iterator(begin());
		}

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}

		list(const list<T>& li)
		{
			empty();
			for (const auto& e : li)
			{
				push_back(e);
			}
			_size = li.size();
		}

		list(initializer_list<T> il)
		{
			empty();
			for (const auto& e : il)
			{
				push_back(e);
			}
			_size = il.size();
		}

		void swap(list<T>& li)
		{
			std::swap(_head, li._head);
			std::swap(_size, li._size);
		}

		list<T>& operator=(list<T> li)
		{
			swap(li);
			return *this;
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insert(begin(), x);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		iterator insert(iterator pos, const T& x)
		{
			Node* tem = pos._node->_prev;
			Node* temnext = pos._node;
			Node* newnode = new Node(x);
			tem->_next = newnode;
			newnode->_prev = tem;
			temnext->_prev = newnode;
			newnode->_next = temnext;

			_size++;

			return iterator(newnode);
		}

		iterator erase(iterator pos)
		{
			Node* tem1 = pos._node->_prev;
			Node* tem2 = pos._node->_next;
			tem1->_next = tem2;
			tem2->_prev = tem1;

			_size--;

			delete pos._node;
			return iterator(tem2);
		}

		size_t size() const
		{
			return _size;
		}

		void empty()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
	private:
		Node* _head;
		size_t _size;
	};
}